package com.steria.workshop.android.yrapp.lib.saxparser;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.steria.workshop.android.yrapp.lib.model.WeatherData;
import com.steria.workshop.android.yrapp.lib.model.WeatherPeriod;
import com.steria.workshop.android.yrapp.lib.model.WeatherPoint;

public class YrSaxHandler extends DefaultHandler {

	private static final String TAG_TIME = "time";
	private static final String TAG_FROM = "from";
	private static final String TAG_TO = "to";

	private WeatherData weatherData;
	private WeatherPoint weatherPoint;
	private WeatherPeriod weatherPeriod;

	protected WeatherData getParsedItems() {
		return weatherData;
	}

	@Override
	public void startDocument() throws SAXException {
		weatherData = new WeatherData();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

		if (localName.equals(TAG_TIME)) {
			if (attributes.getValue(TAG_FROM).equals(attributes.getValue(TAG_TO))) {
				weatherPoint = new WeatherPoint();
				weatherPoint.setDate(stringDateToDate(attributes.getValue(TAG_FROM)));
			} else {
				weatherPeriod = new WeatherPeriod();
				weatherPeriod.setFrom(stringDateToDate(attributes.getValue(TAG_FROM)));
				weatherPeriod.setTo(stringDateToDate(attributes.getValue(TAG_TO)));
			}
		}

		if (localName.equals("temperature"))
			weatherPoint.setTemperature(Double.valueOf(attributes.getValue("value")));

		if (localName.equals("windDirection"))
			weatherPoint.setWindDirection(Double.valueOf(attributes.getValue("deg")));

		if (localName.equals("windSpeed"))
			weatherPoint.setWindSpeed(Double.valueOf(attributes.getValue("mps")));

		if (localName.equals("humidity"))
			weatherPoint.setHumidity(Double.valueOf(attributes.getValue("value")));

		if (localName.equals("pressure"))
			weatherPoint.setPressure(Double.valueOf(attributes.getValue("value")));

		if (localName.equals("coudiness"))
			weatherPoint.setCloudiness(Double.valueOf(attributes.getValue("percent")));

		if (localName.equals("precipitation"))
			weatherPeriod.setPrecipitation(Double.valueOf(attributes.getValue("value")));

		if (localName.equals("symbol"))
			weatherPeriod.setSymbol(attributes.getValue("id"));

	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if (localName.equals(TAG_TIME)) {
			if (weatherPoint != null) {
				weatherData.addWeatherAtPointInTime(weatherPoint);
				Log.d("YrApp", weatherPoint.toString());
			} else if (weatherPeriod != null) {
				weatherData.addWeatherPeriod(weatherPeriod);
			}
			weatherPeriod = null;
			weatherPoint = null;
		}

	}

	private Date stringDateToDate(String dateString) {

		// 2011-04-16T10:48:12Z
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		try {
			Date date = format.parse(dateString);
			date.setHours(date.getHours() + 2);
			return date;
		} catch (ParseException e) {
			Log.e("YrApp", e.getMessage());
		}
		return null;
	}
}
